<template>
  <transition name="slide">
    <div class="search">
      <div class="search-box-wrapper">
        <searchBox @query="onChangequery" ref="searchBox" />
      </div>
       <div ref="shortcutWrapper" class="shortcut-wrapper">
        <Scroll ref="shortcut" class="shortcut" :data="hotWord">
          <div>
            <div class="hot-key" >
              <h1 class="title">热门搜索</h1>
              <ul>
                <li class="item" @click="addQuery(item.searchWord)" v-for="(item,index) in hotWord" :key="index">
                  <span class="top" :class="{'tophot':index<3}">{{index + 1}}</span>
                  <div style="display:inline-block;margin-left:10px;">
                    <span class="searchWord">{{item.searchWord}}</span>
                    <span class="score">{{item.score}}</span>
                    <em class="hot" v-show="item.iconType">HOT!</em>
                    <div class="content">
                      {{item.content}}
                    </div>
                  </div>
                  <!-- <em class="new" v-show="!item.iconType">NEW</em> -->
                </li>
              </ul>
            </div>
          </div>
          <!-- <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span class="clear">
                <i class="icon-clear"></i>
              </span>
            </h1>
            <search-list :searches="searchHistory"></search-list>
          </div> -->
        </Scroll>
        <div class="search-result" v-show="query" ref="searchResult">
          <suggest @select="saveSearch" ref="suggest" :query="query" @listScroll="blurInput"></suggest>
        </div>
    </div>
    </div>
  </transition>
</template>

<script>
import searchBox from 'base/search-box/search-box'
import suggest from 'components/suggest/suggest'
import Scroll from 'base/scroll/scroll'
import {getHotSearch} from 'api/search'
import searchList from 'base/search-list/search-list'
import {mapMutations,mapActions,mapGetters} from 'vuex'

export default {
  components: {
    searchBox,
    suggest,
    searchList,
    Scroll,
  },
  data(){
    return {
      hotWord: [],
      query: ''
    }
  },
  mounted() {
    getHotSearch().then(res => {
      this.hotWord =  res.data.body.data
      console.log(this.hotWord)
      console.log(this.searchHistory)
    })
  },
  computed:{
    ...mapGetters([
      'searchHistory'
    ])
  },
  methods: {
    blurInput() {
      this.$res.searchBox.blur()
    },
    onChangequery(newVal) {
      this.query = newVal
    },
    addQuery(query) {
      this.$refs.searchBox.setQuery(query)
      this.saveSearch(query)
    },
    saveSearch(query) {
      //保存搜索关键词
      this.saveSearchHistory(query)
    },
    ...mapActions([
      'saveSearchHistory'
    ])
  }
}

</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/mixin"
  @import "~common/stylus/variable"

  .search
    position: fixed
    left 0
    right 0
    top 0
    bottom 0
    background: $color-highlight-background
    .search-box-wrapper
      margin: 0px
    .shortcut-wrapper
      position: fixed
      top: 65px
      bottom: 0
      width: 100%
      .shortcut
        height: 100%
        overflow: hidden
        .hot-key
          margin: 0 20px 20px 20px
          .title
            margin-bottom: 20px
            font-size: $font-size-medium
            color: $color-text-l
          .item
            display: block
            padding: 5px 10px
            margin: 0 20px 0px 0
            border-radius: 6px
            background: $color-highlight-background
            font-size: $font-size-medium
            color: $color-text-d
            display flex
            .top
              vertical-align top
            .tophot
              color $color-sub-theme
              font-weight bold
              font-size 16px
            .searchWord
              font-size 16px
              font-weight bold
              color $color-h
              display inline-block
              width 6rem
              overflow hidden
              text-overflow ellipsis
              white-space nowrap
            .score
              color $color-text-ll
              font-size 10px
              margin-left 5px
            .hot
              color $color-sub-theme
              padding 2px 6px
              margin-left 10px
              font-size 12px
            .new
              color $color-search-new
              padding 2px 6px
              margin-left 10px
              font-size 12px
            .content{
              padding 5px 0px
              width 16rem
              overflow hidden
              text-overflow ellipsis
              white-space nowrap
            }
        .search-history
          position: relative
          margin: 0 20px
          .title
            display: flex
            align-items: center
            height: 40px
            font-size: $font-size-medium
            color: $color-text-l
            .text
              flex: 1
            .clear
              extend-click()
              .icon-clear
                font-size: $font-size-medium
                color: $color-text-d
    .search-result
      position: fixed
      width: 100%
      top: 45px
      bottom: 0
      z-index 1
      background #333
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s
  .slide-enter, .slide-leave-to
    transform: translate3d(0, 100%, 0)
</style>

